import java.io.*;
import java.util.Date;

public class FileReadWrite {
	public static final String filename = "file.txt";

	public static void main(String[] args) {
		writeFile();
		readFile(filename);
		getFileEditTime(filename);
		getFileSize(filename);
	}

	public static void writeFile() {
		try {
			BufferedWriter out = new BufferedWriter(new FileWriter(filename));
			out.write("Content：JavaFileDemo\r\nBy：xuxueqian-2019.5.22-周三");
			out.close();
			System.out.println("文件创建成功!");
		} catch (IOException e) {
		}
	}

	public static void readFile(String filename) {
		try {
			BufferedReader in = new BufferedReader(new FileReader(filename));
			String str;
			while ((str = in.readLine()) != null) {
				System.out.println(str);
			}
		} catch (IOException e) {
		}
	}

	public static void getFileEditTime(String filename) {
		File file = new File(filename);
		Long lastModified = file.lastModified();
		Date date = new Date(lastModified);
		System.out.println("上次文件编辑时间：" + date);
	}

	public static void getFileSize(String filename) {
		File file = new File(filename);
		if (!file.exists() || !file.isFile()) {
			System.out.println("文件不存在!");
			return;
		}
		System.out.println("文件大小：" + file.length() + "字节");
	}
}
